/*
 * Copyright (C) 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.idearth.TouchTalk.Directory;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DirectoryDbAdapter {
	
	public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_IP = "ip";
    public static final String KEY_LAST_DATE = "last_date";
    private static final String TAG = "DirectoryDbAdapter";
    private DatabaseHelper DbHelper;
    private SQLiteDatabase Db;

    private static final String DATABASE_NAME = "idearth";
    private static final String DATABASE_TABLE = "directory";
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_CREATE =
            "create table "+ DATABASE_TABLE +" (_id integer primary key autoincrement, "
                    + "name text not null, phone text, ip text, last_date text);";

    private final Context dirCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    public DirectoryDbAdapter(Context ctx) {
        this.dirCtx = ctx;
    }

    public DirectoryDbAdapter open() throws SQLException {
        DbHelper = new DatabaseHelper(dirCtx);
        Db = DbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        DbHelper.close();
    }

    public long createContact(String name, String phone, String ip, String last_date) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_PHONE, phone);
        initialValues.put(KEY_IP, ip);
        initialValues.put(KEY_LAST_DATE, last_date);
        return Db.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteContact(long rowId) {

        return Db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllContacts() {

        return Db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                KEY_PHONE, KEY_IP, KEY_LAST_DATE}, null, null, null, null, null);
    }

    public Cursor fetchContact(long rowId) throws SQLException {

        Cursor mCursor =

                Db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                        KEY_NAME, KEY_PHONE, KEY_IP, KEY_LAST_DATE}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateContact(long rowId, String name, String phone, String ip, String last_date) {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_PHONE, phone);
        args.put(KEY_IP, ip);
        args.put(KEY_LAST_DATE, last_date);
        return Db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
